package com.fy56.platform.goodsTracking.commons.unit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {

    public final static String DATE_FORMAT = "yyyy-MM-dd";
    public final static String DATE_FORMAT_CN = "yyyy年MM月dd日";
    public final static String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public final static String TIME_FORMAT_CN = "yyyy年MM月dd日 HH:mm:ss";
    public final static String MONTH_FORMAT = "yyyy-MM";
    public final static String DATE_FORMAT_EN = "yyyy.MM.dd";
    public final static String DATA_FORMAT_ON = "yy.MM.dd";
    public final static String DATA_FORMAT_MM = "yyyyMMdd";
    public final static String TIME_FORMAT_14 = "yyyyMMddHHmmss";

    public final static String Hour_FORMAT = "HHmm";

    /**
     * qsj MM/dd/yyyy HH:mm:ss to yyyy-MM-dd HH:mm:ss
     *
     * @param time
     * @return
     */
    public static String getForTime(String time) {
        String str1 = "";
        String str2 = "";
        if (time != null && time.contains("/")) {
            str1 = time.substring(0, time.lastIndexOf("/") + 1);
            // System.out.println(str1);
            str2 = time.replace(str1, "");
            // System.out.println(str2);
            time = str2.replace(" ", "/" + str1.substring(0, str1.length() - 1)
                    + " ");
            // System.out.println(time.replace("/", "-"));
            time = time.replace("/", "-");
        } else if (time != null && time.length() > 19) {
            time = time.substring(0, 19);
        }
        return time;
    }

    public static String getFormatDate_EN(Date currDate) {
        SimpleDateFormat f = new SimpleDateFormat(DATE_FORMAT_EN);
        String time = f.format(currDate);
        return time;
    }

    public static String getFormatDate_ON(Date currDate) {
        SimpleDateFormat f = new SimpleDateFormat(DATA_FORMAT_ON);
        String time = f.format(currDate);
        return time;
    }

    public static Date getCurrDate() {
        return new Date();
    }

    public static java.sql.Timestamp getCurrTimestamp() {
        return new java.sql.Timestamp(System.currentTimeMillis());
    }

    public static String getFormatDate(Date currDate) {
        return getFormatDate(currDate, DATE_FORMAT);
    }

    public static Date getFormatDateToDate(Date currDate) {
        return getFormatDate(getFormatDate(currDate));
    }

    public static String getFormatDate_CN(Date currDate) {
        return getFormatDate(currDate, DATE_FORMAT_CN);
    }

    public static Date getFormatDateToDate_CN(Date currDate) {
        return getFormatDate_CN(getFormatDate_CN(currDate));
    }

    public static Date getFormatDate(String currDate) {
        try {
            return getFormatDate(currDate, DATE_FORMAT);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static Date getFormatDate_CN(String currDate) {
        try {
            return getFormatDate(currDate, DATE_FORMAT_CN);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static String getFormatDate(Date currDate, String format) {
        SimpleDateFormat dtFormatdB = null;
        try {
            dtFormatdB = new SimpleDateFormat(format);
            return dtFormatdB.format(currDate);
        } catch (Exception e) {
            dtFormatdB = new SimpleDateFormat(DATE_FORMAT);
            return dtFormatdB.format(currDate);
        }
    }

    public static String getFormatDateTime(Date currDate) {
        return getFormatDateTime(currDate, TIME_FORMAT);
    }

    public static Date getFormatDateTimeToTime(Date currDate) {
        return getFormatDateTime(getFormatDateTime(currDate));
    }

    public static Date getFormatDateTime(String currDate) {
        try {
            return getFormatDateTime(currDate, TIME_FORMAT);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static String getFormatDateTime_CN(Date currDate) {
        return getFormatDateTime(currDate, TIME_FORMAT_CN);
    }

    public static Date getFormatDateTimeToTime_CN(Date currDate) {
        return getFormatDateTime_CN(getFormatDateTime_CN(currDate));
    }

    public static Date getFormatDateTime_CN(String currDate) {
        try {
            return getFormatDateTime(currDate, TIME_FORMAT_CN);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getFormatDateTime(Date currDate,
                                           String format) {
        SimpleDateFormat dtFormatdB = null;
        try {
            dtFormatdB = new SimpleDateFormat(format);
            return dtFormatdB.format(currDate);
        } catch (Exception e) {
            dtFormatdB = new SimpleDateFormat(TIME_FORMAT);
            return dtFormatdB.format(currDate);
        }
    }

    public static Date getFormatDate(String currDate, String format)
            throws Exception {
        SimpleDateFormat dtFormatdB = null;
        dtFormatdB = new SimpleDateFormat(format);
        return dtFormatdB.parse(currDate);
    }

    public static Date getFormatDateTime(String currDate, String format)
            throws Exception {
        SimpleDateFormat dtFormatdB = null;
        dtFormatdB = new SimpleDateFormat(format);
        return dtFormatdB.parse(currDate);
    }

    public static String getCurrDateStr() {
        return getFormatDate(getCurrDate());
    }

    public static String getCurrDateMM() {
        return getFormatDate(getCurrDate(), DATA_FORMAT_MM);
    }

    public static String getCurrDate14() {
        return getFormatDate(getCurrDate(), TIME_FORMAT_14);
    }

    public static String getCurrDateTimeStr() {
        return getFormatDateTime(getCurrDate());
    }

    public static String getCurrDateStr_CN() {
        return getFormatDate(getCurrDate(), DATE_FORMAT_CN);
    }

    public static String getCurrDateTimeStr_CN() {
        return getFormatDateTime(getCurrDate(), TIME_FORMAT_CN);
    }

    public static Date getDateBeforeOrAfter(int iDate) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, iDate);
        return cal.getTime();
    }

    public static Date getDateBeforeOrAfter(Date curDate, int iDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(curDate);
        cal.add(Calendar.DAY_OF_MONTH, iDate);
        return cal.getTime();
    }

    public static String getFormatMonth(Date currDate) {
        return getFormatDate(currDate, MONTH_FORMAT);
    }

    public static String getFirstDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        int firstDay = cal.getMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        return getFormatDate(cal.getTime(), DATE_FORMAT);
    }

    public static String getLastFirstDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        int firstDay = cal.getMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        cal.add(Calendar.MONTH, -1);
        return getFormatDate(cal.getTime(), DATE_FORMAT);
    }

    public static String getFirstDayOfMonth(Date currDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(currDate);
        int firstDay = cal.getMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        return getFormatDate(cal.getTime(), DATE_FORMAT);
    }

    public static String getFormatFromStringByFormatType(String dateStr, String oldFormatType, String newFormatType) throws ParseException {
        SimpleDateFormat oldFormat = new SimpleDateFormat(oldFormatType);
        SimpleDateFormat newFormat = new SimpleDateFormat(newFormatType);
        Date date = oldFormat.parse(dateStr);
        return newFormat.format(date);
    }

    /**
     * 获取某月的第一天或最后一天
     *
     * @param date
     * @param flag 1为第一天，-1为最后一天
     * @return
     */
    public static Date dayOfMonth(Date date,int flag){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if(flag == -1){
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getMaximum(Calendar.DAY_OF_MONTH));
        }else{
            calendar.set(Calendar.DAY_OF_MONTH, flag);
        }
        return calendar.getTime();
    }

    /**
     * 获取两个日期相差的天数
     *
     * @param fromDate
     * @param toDate
     * @return
     */
    public static Long getDiffDays(Date fromDate,Date toDate){
        return Long.valueOf ((toDate.getTime() - fromDate.getTime())/(24*60*60*1000));
    }

    /**
     * 获取两个日期相差的天数
     *
     * @param fromDateStr
     * @param toDateStr
     * @return
     */
    public static Long getDiffDays(String fromDateStr,String toDateStr) throws Exception {
        Date fromDate = getFormatDate(fromDateStr,DATE_FORMAT);
        Date toDate = getFormatDate(toDateStr,DATE_FORMAT);
        return getDiffDays(fromDate,toDate);
    }

    public static String getCurrHourStr(){
        SimpleDateFormat st = new SimpleDateFormat(Hour_FORMAT);
        return st.format(new Date());
    }

    public static int getHour(Date time){
        SimpleDateFormat st = new SimpleDateFormat(Hour_FORMAT);
        return Integer.parseInt(st.format(time));
    }

    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
}
